package com.wenhuitec.sysui.view;

import android.content.Context;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.sysui.R;
import com.wenhuitec.sysui.adapter.FoodRecyclerAdapter;
import com.wenhuitec.sysui.entity.Foods;

import java.math.BigDecimal;
import java.util.List;

public class FoodView extends LinearLayout {

    TextView title;
    private RecyclerView mRecycleView;
    private LinearLayoutManager mLinearLayoutManager;//布局管理器
    private FoodRecyclerAdapter mAdapter;//适配器

    LinearLayout developLinear;
    TextView zkBut;
    TextView amountText;

    private boolean develop = false;

    public FoodView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_foods, this, true);
        title = (TextView) findViewById(R.id.title);
        zkBut = (TextView) findViewById(R.id.zk_but);
        amountText = (TextView) findViewById(R.id.amount_text);
        developLinear = (LinearLayout) findViewById(R.id.developLinear);
        zkBut.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (develop) {
                    develop = false;
                    developLinear.setVisibility(VISIBLE);
                    zkBut.setText("收起");
                } else {
                    develop = true;
                    developLinear.setVisibility(GONE);
                    zkBut.setText("展开");
                }
            }
        });
        mRecycleView = findViewById(R.id.rv_list);
        //创建布局管理器，垂直设置LinearLayoutManager.VERTICAL，水平设置LinearLayoutManager.HORIZONTAL
        mLinearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
    }


    public void setFoods(List<Foods> foods) {

        //创建适配器，将数据传递给适配器
        mAdapter = new FoodRecyclerAdapter(foods);
        //设置布局管理器
        mRecycleView.setLayoutManager(mLinearLayoutManager);
        //设置适配器adapter
        mRecycleView.setAdapter(mAdapter);
        title.setText(  foods.size() + "  商品");
        BigDecimal price = new BigDecimal(0);
        for (Foods goo : foods) {
            price = price.add(new BigDecimal(goo.getPrice()));
        }
        amountText.setText("￥"+price.toPlainString());
    }


}
